/**
 * 研发项目状态机配置
 */

// 定义状态
const STATES = {
  // 基本状态
  'pending': { name: '待立项', color: '#909399', description: '项目正在等待立项' },
  'review': { name: '项目审核', color: '#E6A23C', description: '项目正在审核中' },
  'active': { name: '活动', color: '#409EFF', description: '项目处于活动状态' },
  'dormant': { name: '休眠', color: '#909399', description: '项目处于休眠状态' },
  'completed': { name: '完成', color: '#67C23A', description: '项目已完成' },
  
  // 活动子状态
  'active.hw_confirm': { name: '硬件确认', color: '#409EFF', description: '正在进行硬件确认' },
  'active.hw_design': { name: '硬件设计打样', color: '#409EFF', description: '正在进行硬件设计打样' },
  'active.debug': { name: '软硬件调试', color: '#409EFF', description: '正在进行软硬件调试' },
  'active.testing': { name: '测试验证', color: '#409EFF', description: '正在进行测试验证' },
  'active.customer_confirm': { name: '客户确认', color: '#409EFF', description: '等待客户确认' },
  'active.order_preparation': { name: '订单准备', color: '#409EFF', description: '正在准备订单' },
  'active.production': { name: '生产', color: '#409EFF', description: '正在生产中' },
  'active.feedback': { name: '产品待客户反馈', color: '#409EFF', description: '等待客户反馈' }
};

// 定义事件
const EVENTS = {
  'submit': { name: '提交申请', description: '提交项目申请' },
  'approve': { name: '批准', description: '批准项目或状态变更' },
  'reject': { name: '拒绝', description: '拒绝项目或状态变更' },
  'start': { name: '启动', description: '启动项目' },
  'suspend': { name: '暂停', description: '暂停项目' },
  'resume': { name: '恢复', description: '恢复项目' },
  'complete': { name: '完成', description: '完成项目' },
  
  // 活动状态相关事件
  'hw_confirm': { name: '硬件确认', description: '进入硬件确认阶段' },
  'hw_design': { name: '硬件设计', description: '进入硬件设计阶段' },
  'debug': { name: '软硬件调试', description: '进入软硬件调试阶段' },
  'test': { name: '测试', description: '进入测试阶段' },
  'customer_confirm': { name: '客户确认', description: '进入客户确认阶段' },
  'prepare_order': { name: '准备订单', description: '进入订单准备阶段' },
  'production': { name: '生产', description: '进入生产阶段' },
  'feedback': { name: '客户反馈', description: '进入客户反馈阶段' }
};

// 状态变迁定义
const STATE_TRANSITIONS = {
  'pending': {
    'submit': 'review'
  },
  'review': {
    'approve': 'active',
    'reject': 'pending'
  },
  'active': {
    'suspend': 'dormant',
    'complete': 'completed',
    'hw_confirm': 'active.hw_confirm',
    'hw_design': 'active.hw_design',
    'debug': 'active.debug',
    'test': 'active.testing',
    'customer_confirm': 'active.customer_confirm',
    'prepare_order': 'active.order_preparation',
    'production': 'active.production',
    'feedback': 'active.feedback'
  },
  'dormant': {
    'resume': 'active'
  },
  'completed': {
    'resume': 'active'
  },
  'active.hw_confirm': {
    'approve': 'active.hw_design',
    'suspend': 'dormant'
  },
  'active.hw_design': {
    'approve': 'active.debug',
    'suspend': 'dormant'
  },
  'active.debug': {
    'approve': 'active.testing',
    'suspend': 'dormant'
  },
  'active.testing': {
    'approve': 'active.customer_confirm',
    'suspend': 'dormant'
  },
  'active.customer_confirm': {
    'approve': 'active.order_preparation',
    'suspend': 'dormant'
  },
  'active.order_preparation': {
    'approve': 'active.production',
    'suspend': 'dormant'
  },
  'active.production': {
    'approve': 'active.feedback',
    'suspend': 'dormant'
  },
  'active.feedback': {
    'approve': 'completed',
    'suspend': 'dormant'
  }
};

// 获取状态名称
function getStateName(state) {
  if (!state || !STATES[state]) return '未知状态';
  return STATES[state].name;
}

// 获取状态颜色
function getStateColor(state) {
  if (!state || !STATES[state]) return '#909399';
  return STATES[state].color;
}

// 获取事件名称
function getEventName(event) {
  if (!event || !EVENTS[event]) return '未知事件';
  return EVENTS[event].name;
}

// 是否允许事件
function isEventAvailable(state, event) {
  if (!state || !event || !STATE_TRANSITIONS[state]) return false;
  return !!STATE_TRANSITIONS[state][event];
}

// 获取下一个状态
function getNextState(state, event) {
  if (!state || !event || !STATE_TRANSITIONS[state] || !STATE_TRANSITIONS[state][event]) return null;
  return STATE_TRANSITIONS[state][event];
}

// 获取当前状态可用的事件
function getAvailableEvents(state) {
  if (!state || !STATE_TRANSITIONS[state]) return [];
  
  const events = [];
  for (const eventId in STATE_TRANSITIONS[state]) {
    if (STATE_TRANSITIONS[state].hasOwnProperty(eventId)) {
      events.push({
        id: eventId,
        name: getEventName(eventId),
        nextState: STATE_TRANSITIONS[state][eventId]
      });
    }
  }
  
  return events;
}

// 根据状态获取可能的事件
function getEventsByStatus(state) {
  return getAvailableEvents(state);
}

module.exports = {
  STATES,
  EVENTS,
  STATE_TRANSITIONS,
  getStateName,
  getStateColor,
  getEventName,
  isEventAvailable,
  getNextState,
  getAvailableEvents,
  getEventsByStatus
}; 